package com.meta.nd.vo.json;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.meta.common.ConstDef;
import com.meta.nd.util.NodeUtil;
import com.meta.nd.vo.Fv;
import com.meta.nd.vo.Nd;
import com.meta.nd.vo.Nv;
import java.io.IOException;
import java.util.Date;

public class NvSerializer extends JsonSerializer<Nv> {
    @Override
    public void serialize(Nv nv, JsonGenerator jgen, SerializerProvider provider)
            throws IOException, JsonProcessingException {
        jgen.writeStartObject();
        jgen.writeNumberField("id",nv.getId());
        jgen.writeStringField("operationFlag", nv.getOperationFlag());
        jgen.writeNumberField("parentId", nv.getParentId());
        Nd nd = nv.getNd();
        jgen.writeStringField("__ndCode", nd.getNdCode());
        jgen.writeStringField("__ndName", nd.getNdName());
        jgen.writeStringField("__dbMapCode", nd.getDbMapCode());
        jgen.writeStringField("__actualNdCode", nv.getActualNdCode());
        for (Fv fv : nv.getFvs().values() ) {
            if ( fv.getDataType().equals(ConstDef.DATA_TYPE_DATE))
                jgen.writeObjectField(fv.getName(), NodeUtil.formatDate((Date)fv.getValue()));
            else if ( fv.getDataType().equals(ConstDef.DATA_TYPE_DATETIME))
                jgen.writeObjectField(fv.getName(), NodeUtil.formatDatetime((Date)fv.getValue()));
            else
                jgen.writeObjectField(fv.getName(), fv.getValue());
        }
        for ( String name : nv.getChildrenMap().keySet()) {
            jgen.writeObjectField(name, nv.getChildNvs(name));
        }
        jgen.writeEndObject();
    }
}
